<?php

class Mainjobmanager extends Controller {

	function Mainjobmanager()
	{
		parent::Controller();

		$this->db->query('set names utf8');

		$this->authlib->check();
	}

	// 初始化新增对话框与编辑对话框中的下拉列表框等的数据
	function _initSystemparam()
	{
		$this->load->model('Districtmodel');
		$this->load->model('Systemparam');

		$data['district1'] = $this->Districtmodel->getDistrictByLevel(1);
		// 用途下拉列表
		$data['PURPOSE'] = $this->Systemparam->getParamByParentId(7);
		// 朝向下拉列表
		$data['DIRECTION'] = $this->Systemparam->getParamByParentId(11);
		// 房源状态下拉列表
		$data['status'] = $this->Systemparam->getParamByParentId(15);
		// 装修下拉列表
		$data['fitment'] = $this->Systemparam->getParamByParentId(12);
		// 家俱下拉列表
		$data['jiaju'] = $this->Systemparam->getParamByParentId(17);
		// 家电下拉列表
		$data['jiadian'] = $this->Systemparam->getParamByParentId(18);
		// 来源下拉列表
		$data['SOURCE'] = $this->Systemparam->getParamByParentId(16);
		// 付佣下拉列表
		$data['fuyong'] = $this->Systemparam->getParamByParentId(10);
		// 产权下拉列表
		$data['property'] = $this->Systemparam->getParamByParentId(19);
		// 证件下拉列表
		$data['zhengjian'] = $this->Systemparam->getParamByParentId(20);
		// 租售单位下拉列表
		$data['unit'] = $this->Systemparam->getParamByParentId(24);
		// 付款下拉列表
		$data['pay_type'] = $this->Systemparam->getParamByParentId(13);
		// 货币下拉列表
		$data['money_type'] = $this->Systemparam->getParamByParentId(14);
		// 委托方式下拉列表
		$data['CONSIGN_TYPE'] = $this->Systemparam->getParamByParentId(22);
		// 房间配置列表
		$data['kit_memo'] = $this->Systemparam->getParamByParentId(25);
		// 配套设施列表
		$data['kit_info'] = $this->Systemparam->getParamByParentId(26);

		return $data;
	}
	
	
	// 构建房源管理界面
	function index()
	{
		$this->load->model('Systemparam');

		// 景观下拉列表
		$data['jingguan'] = $this->Systemparam->getParamByParentId(27);
		// 用途下拉列表
		$data['yongtu'] = $this->Systemparam->getParamByParentId(7);
		$this->load->view('organization/mainjobmanager', $data);
	}
	
	// 根据条件查询房源信息，返回XML
	function search()
	{
		$this->load->helper('xml');
		$this->load->model('Mainjobmodel');

		// Load the mime types
		@include(APPPATH.'config/mimes'.EXT);

		header("Content-Type: {$mimes['xml']};");
		header("Cache-Control: no-cache, must-revalidate");
		header("Pragma: no-cache");
		
		$output = '<?xml version="1.0" encoding="UTF-8" ?>';
		//if (!empty($_POST['data']))
		//	parse_str($_POST['data'], $condition);
		// 取出分页之前的总数
		$num = $this->Mainjobmodel->search($_POST, true);
		$num = $num[0]->count;
		$data = $this->Mainjobmodel->search($_POST);
		
		$output .= "<SearchReponse>";
		if (!empty($data))
		{
			$output .= "<total>" . $num . "</total>\n";
			foreach ($data as $item)
			{
				$output .= "<Items>\n";
				$output .= "<Item>\n";
				foreach ($item as $key => $value)
				{
					if ($key == 'date')
					{
						$value = date("Y_M_D H:I:S",$value);
					}
					$output .= "<$key>".xml_convert($value)."</$key>\n";
				}
//				$output .= "<Mainjobmodel>{$item->house_fang}室{$item->house_ting}厅{$item->house_wei}卫</Mainjobmodel>\n";
				$output .= "</Item>\n";
				$output .= "</Items>";
			}
		}
		$output .= '</SearchReponse>';

		$this->output->set_output($output);
	}

	// 根据地区上级ID查询地区信息，返回XML
	function getDistrict($pid)
	{
		$this->load->model('Districtmodel');

		$output = $this->Districtmodel->getDistrictXml($pid);

		$this->output->set_output($output);
	}

	// 根据顶级ID查询下级所有地区信息，返回XML
	function getAllDistrict($id)
	{
		$this->load->model('Districtmodel');

		$output = $this->Districtmodel->getAllDistrictXml($id);

		$this->output->set_output($output);
	}

	// 显示房源相信信息，返回HTML
	function detail($id)
	{
		$this->load->model('Mainjobmodel');

		$data['detail'] = $this->Mainjobmodel->getHouseById($id);
		$data['if_encrypt_buildsrc'] = '';
		$data['if_encrypt_dept'] = '';
		$data['if_KEY'] = '';
		$data['if_attic'] = '';
		$data['if_elevator'] = '';
		if ($data['detail']->if_encrypt_buildsrc == 1)
		{
			$data['if_encrypt_buildsrc'] = 'checked="checked"';
		}
		if ($data['detail']->if_encrypt_dept == 1)
		{
			$data['if_encrypt_dept'] = 'checked="checked"';
		}
		if ($data['detail']->if_KEY == 1)
		{
			$data['if_KEY'] = 'checked="checked"';
		}
		if ($data['detail']->if_attic == 1)
		{
			$data['if_attic'] = 'checked="checked"';
		}
		if ($data['detail']->if_elevator == 1)
		{
			$data['if_elevator'] = 'checked="checked"';
		}
//		$this->load->view('house/manager_detail', $data);
	}

	// 新增房源
	function add()
	{
		$data = $this->_initSystemparam();
		$data['mode'] = 'add';
		$data['editdata'] = '';

//		$this->load->view('house/manager_dialog', $data);
	}

	// 保存新增房源
	function addsave()
	{
		$this->load->model('Mainjobmodel');

		// Load the mime types
		@include(APPPATH.'config/mimes'.EXT);

		header("Content-Type: {$mimes['xml']};");
		header("Cache-Control: no-cache, must-revalidate");
		header("Pragma: no-cache");

		//if (!empty($_POST['data']))
		//	parse_str($_POST['data'], $condition);
		$data = $this->Mainjobmodel->add($_POST);

		$output = '<?xml version="1.0"?><response><SaveResponse>';
		if ($data==true)
		{
			$output .= 'true';
		}
		else
		{
			$output .= 'false';
		}
		$output .= '</SaveResponse></response>';
		$this->output->set_output($output);
	}

	// 编辑房源
	function edit($id)
	{
		$this->load->model('Mainjobmodel');

		$data = $this->_initSystemparam();
		$data['mode'] = 'edit';
		$result = $this->Mainjobmodel->getHouseById($id);
		$result->district1id = $this->Districtmodel->getDistrictIdByName(0, $result->district1);
		$result->district2id = $this->Districtmodel->getDistrictIdByName($result->district1id, $result->district2);
		$data['editdata'] = $result;

		$this->load->view('house/manager_dialog', $data);
	}

	// 保存编辑房源
	function editsave()
	{
		$this->load->model('Mainjobmodel');

		// Load the mime types
		@include(APPPATH.'config/mimes'.EXT);

		header("Content-Type: {$mimes['xml']};");
		header("Cache-Control: no-cache, must-revalidate");
		header("Pragma: no-cache");

		//if (!empty($_POST['data']))
		//	parse_str($_POST['data'], $condition);
		$data = $this->Mainjobmodel->edit($_POST);

		$output = '<?xml version="1.0"?><response><SaveResponse>';
		if ($data==true)
		{
			$output .= 'true';
		}
		else
		{
			$output .= 'false';
		}
		$output .= '</SaveResponse></response>';
		$this->output->set_output($output);
	}
	
	// 删除房源
	function delete()
	{
		$this->load->model('Mainjobmodel');

		// Load the mime types
		@include(APPPATH.'config/mimes'.EXT);

		header("Content-Type: {$mimes['xml']};");
		header("Cache-Control: no-cache, must-revalidate");
		header("Pragma: no-cache");

		$param = explode(",", $_POST['data']);
		$data = $this->Mainjobmodel->delete($param);

		$output = '<?xml version="1.0"?><response><SaveResponse>';
		if ($data==true)
		{
			$output .= 'true';
		}
		else
		{
			$output .= 'false';
		}
		$output .= '</SaveResponse></response>';
		$this->output->set_output($output);
		
	}
}
?>